package com.gg.util;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import org.springframework.amqp.core.QueueInformation;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Delivery;
import com.rabbitmq.client.Envelope;

public class Consumer {
	
	
	public static void main(String[] args) throws IOException, TimeoutException {
		
		//1创建ConnectionFactory
		ConnectionFactory connectionFactory = new ConnectionFactory();
		connectionFactory.setHost("127.0.0.1");
		connectionFactory.setPort(5672);
		connectionFactory.setVirtualHost("/");
		
		//2创建链接
		Connection connection = connectionFactory.newConnection();
		
		//3创建channel
		Channel channel = connection.createChannel();
		
		//4创建一个队列
		/* --参数--
		 * 1queue 消息队列名字
		 * 2durable 重启服务的时候是否存在  true是  false否
		 * 3exclusive 是否为独占队列
		 * 4autoDelete 无效是否自动删除队列
		 * 5arguments 扩展参数
		 */
		String queueName = "im' consumer";
		channel.queueDeclare(queueName, true, false, false, null);
		
		//5创建消费者
		/*
		 * springboot从1.5.9升级到2.0.0，queueingconsumer报错没有这个类，改为使用 DefaultConsumer
		 */
		DefaultConsumer defaultConsumer = new DefaultConsumer(channel);
		//6.设置channel
		
		/* --参数--
		 * 1queueName
		 * 2是否为自动签收（true为自动告诉服务端是否收到了消息）
		 * 3消费者对象
		 */
		channel.basicConsume(queueName, true, defaultConsumer);
		
		//7获取消息
		//Delivery delivery = defaultConsumer.handleDelivery(consumerTag, envelope, properties, body);
		System.out.println(defaultConsumer.getConsumerTag());;
	
	}
	 
}
